package hu.plusone.eregister.controller;

import hu.plusone.eregister.model.Examiner;
import hu.plusone.eregister.service.ExaminerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/examiner")
public class ExminerController {

    private static final Logger logger = LoggerFactory.getLogger(ExminerController.class);
    private static final String PAGE_LIST = "examiner/list";
    private static final String PAGE_EDIT = "examiner/edit";
    
    @Autowired
    private ExaminerService service;

    @RequestMapping("/list")
    public String listAll(Model model) {
        model.addAttribute("modelList", service.getAll());
        return PAGE_LIST;
    }

    @RequestMapping("/delete")
    public String delete(Model model, @RequestParam long id) {
        service.delete(id);
        return listAll(model);
    }

    
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String editForm(Model model, @RequestParam(required = false) Long id) {
        Examiner entity;
        if (id == null) {
            entity = new Examiner();
        } else {
            entity = service.get(id);
        }
        model.addAttribute("entity", entity);
        return PAGE_EDIT;
    }
    
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String edit(Model model, @ModelAttribute("entity") Examiner entity) {
        service.save(entity);
        return listAll(model);
    }
    
}
